# /usr/bin

for i in `find $2 -type f  -name "IdentityCard.h"` ; do idCardH=$i ; done
for i in `find $2 -type f  -name "*.dico"` ; do dicoFile=$i ; done
for i in `find $2 -type f  -name "*.iid"`   ; do iidFile=$i ; done

# if the second argument is a directory
if [ -d $2 ] ; then 
chmod -R 755 $2
rm -rf $2
fi

# copy dir 1 to dir 2
cp -rf $1 $2
chmod -R 755 $2
# find list of all files in dir 2 
#FilesToCopy.txt format : FullPath(File/Dir)	ParentDir	FileName/DirName
find $2 -type f  -printf "\n%p %h \t %f" > FilesToCopy.txt
find $2 -type d -printf "\n%p %h \t %f" > DirsToCopy.txt

# Change the names of the file
sed 's/$3/$4/g'  FilesToCopy.txt > NewFileNames.tmp
#paste FilesToCopy.txt  NewFileNames.tmp > NewFileNames.txt
#rm -f NewFileNames.tmp

awk ' {} '
# replace the contents of the files
awk -v r=$3 -v Replacement=$4 -f ReplaceTextInFile.awk  FilesToCopy.txt

# rename the files 
awk -v r=$3 -v Replacement=$4 -f BitteGerne.awk  FilesToCopy.txt

# rename the dirs 
awk -v r=$3 -v Replacement=$4 -f BitteGerne.awk  DirsToCopy.txt

# TO DO.
# dico files should be appended
# Identity Card file should be appended - preferably without any duplication.

# The first input file will be processed first and then the 2nd input file
#awk '{ print "moving " $0 " to " $0 }' FilesToCopy.txt NewFileNames.txt
 